﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace PhysicsSimulator
{
    class CPendulum : CGameObject
    {
        private double _alpha;

        public double Alpha
        {
            get { return _alpha; }
            set { _alpha = value; }
        }

        private float _P;

        public float P
        {
            get { return _P; }
            set { _P = value; }
        }
        private float _Pt;

        public float Pt
        {
            get { return _Pt; }
            set { _Pt = value; }
        }
        private float _Pn;

        public float Pn
        {
            get { return _Pn; }
            set { _Pn = value; }
        }
        private float _Q;

        public float Q
        {
            get { return _Q; }
            set { _Q = value; }
        }

        private Texture2D _pendulumTexture;

        public Texture2D PendulumTexture
        {
            get { return _pendulumTexture; }
            set { _pendulumTexture = value; }
        }

        Vector2 _position;

        public Vector2 PendulumPositon
        {
            get { return _position; }
            set { _position = value; }
        }

        float _pendulumScale;

        public float PendulumScale
        {
            get { return _pendulumScale; }
            set { _pendulumScale = value; }
        }

        private float _rotate;

        public float Rotate
        {
            get { return _rotate; }
            set { _rotate = value; }
        }


        CVectorForce P_vector,T_vector,Pt_vector,Pn_vector;

        public CPendulum(SpriteBatch spriteBatch, Vector2 position, float scale) 
            : base(spriteBatch)
        {
            _position = position;
            _pendulumScale = scale;

            
        }

        public override void Init(Microsoft.Xna.Framework.Content.ContentManager contentManager)
        {

            _pendulumTexture = contentManager.Load<Texture2D>("Images/pendulum");


            P_vector = new CVectorForce(SpriteBatch);
            P_vector.Init(contentManager);
            P_vector.Create(_position, Vector2.Zero,2,Color.HotPink);

            T_vector = new CVectorForce(SpriteBatch);
            T_vector.Init(contentManager);

            Pt_vector = new CVectorForce(SpriteBatch);
            Pt_vector.Init(contentManager);
            Pt_vector.Create(_position, Vector2.Zero, 2, Color.YellowGreen);

            Pn_vector = new CVectorForce(SpriteBatch);
            Pn_vector.Init(contentManager);
            Pn_vector.Create(_position, Vector2.Zero, 2, Color.Red);

            T_vector = new CVectorForce(SpriteBatch);
            T_vector.Init(contentManager);
            T_vector.Create(_position, Vector2.Zero, 2, Color.Blue);

            base.Init(contentManager);
        }

        public override void Update(GameTimerEventArgs timer)
        {
            _Pn = (float)(P * Math.Cos(_alpha));
            UpdateVectorForce();
            base.Update(timer);
        }


        public override void Draw(GameTimerEventArgs timer)
        {
            SpriteBatch.Draw(_pendulumTexture, _position, null, Color.White, 0, new Vector2(_pendulumTexture.Width / 2, _pendulumTexture.Height / 2), _pendulumScale, SpriteEffects.None, 0);
            P_vector.Draw(timer);
            Pt_vector.Draw(timer);
            Pn_vector.Draw(timer);
            T_vector.Draw(timer);
            base.Draw(timer);
        }

        void UpdateVectorForce()
        {
            P_vector.Position = _position;
            Pt_vector.Position = _position;
            Pn_vector.Position = _position;
            T_vector.Position = _position;

            float x_P = 0;
            float y_P = _P*5;
            P_vector.VectorForce = new Vector2(x_P, y_P);

            float x_Pn = _Pt *5;
            float y_Pn = _Pn * 5;
            Pn_vector.VectorForce = new Vector2(x_Pn, y_Pn);

            float x_Q = (float)(-Q * 5 * Math.Tan(_alpha));
            float y_Q = - Q * 5;

            T_vector.VectorForce = new Vector2(x_Q, y_Q);

            float x_Pt = -_Pt * 5;
            float y_Pt = 0;
            Pt_vector.VectorForce = new Vector2(x_Pt, y_Pt);

            //float x_Pt = 

        }
    }
}
